const crypto = require('crypto');

// 加密函数
function encrypt(text, key) {
    const cipher = crypto.createCipher('aes-256-cbc', key);
    let encrypted = cipher.update(text, 'utf8', 'hex');
    encrypted += cipher.final('hex');
    return encrypted;
}

// 解密函数
function decrypt(encryptedText, key) {
    const decipher = crypto.createDecipher('aes-256-cbc', key);
    let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;
}

// 示例
const key = 'MySuperSecretKey123'; // 这里使用一个固定的密钥，实际应用中应该更安全地管理密钥
const text = 'Hello, World!';

const encryptedText = encrypt(text, key);
console.log('加密后:', encryptedText);

const decryptedText = decrypt(encryptedText, key);
console.log('解密后:', decryptedText);
